﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GpCommon.Helpers;
using GpCommon.Logger;

namespace FirstLimitUpMode.Data
{
    public class TempData
    {
        public static int CurValue = 5000;
        public static float ValueLimit = 0;

        public static List<string> RemoveList = new List<string>();
        public static List<string> OrderToBuyList = new List<string>();
        public static List<string> OrderToSaleList = new List<string>();
        public static List<string> WishGpList = new List<string>();

        #region 自选
        private static List<string> m_LockList;

        public static List<string> LockList
        {
            get 
            { 
                if(m_LockList == null)
                {
                    m_LockList = GetLockGPs();
                }
                return m_LockList;
            }
            set { m_LockList = value; }
        }


        private static string LockFilePath = "LockFile.txt";
        public static List<string> GetLockGPs()
        {
            List<string> gps = new List<string>();
            try
            {
                if (File.Exists(LockFilePath))
                {
                    string strs = File.ReadAllText(LockFilePath);
                    var codes = strs.Split(';');
                    foreach (var item in codes)
                    {
                        if (!string.IsNullOrEmpty(item))
                            gps.Add(item);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }
            return gps;
        }
        public static bool WriteLockGps()
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                foreach (var item in LockList)
                {
                    sb.Append(item + ";");
                }
                File.WriteAllText(LockFilePath, sb.ToString());
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }
            return true;
        }
        #endregion

        #region 每日记录
        private static List<string> m_DayByDayGpList { get; set; } = new List<string>();
        public static void AddDayByDayGp(string code)
        {
            if (!m_DayByDayGpList.Contains(code))
                m_DayByDayGpList.Add(code);
        }
        public static void WriteDayByDayGps()
        {
            string pathName = string.Format("{0}-{1}-{2}.txt", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
            if(File.Exists(pathName))
            {
                var content = File.ReadAllText(pathName);
                if(!string.IsNullOrEmpty(content))
                {
                    var strs = content.Split(';');
                    foreach (var item in strs)
                    {
                        AddDayByDayGp(item);
                    }
                }
            }
            if (m_DayByDayGpList.Count <= 0) return;

            try
            {
                StringBuilder sb = new StringBuilder();
                foreach (var item in m_DayByDayGpList)
                {
                    sb.Append(item + ";");
                }
                File.WriteAllText(pathName, sb.ToString());

                string imgName = string.Format("{0}-{1}-{2}.png", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
                if(File.Exists(imgName))
                    File.Delete(imgName);
                CommonUtility.ExportGPToImg(m_DayByDayGpList, imgName);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }
        }
        #endregion
    }
}
